﻿
/*=========================================
* Author: Administrator
* DateTime:2017/6/21 17:56:03
* Description:$safeprojectname$
==========================================*/

using System;
using System.Collections.Generic;
using PureMVC.Patterns;
using UnityEngine;

namespace OrderSystem
{
    public class WaiterProxy : Proxy
    {
        public new const string NAME = "WaiterProxy";

        public void SetWaiterWaitClientHello(WaiterItem waiter)
        {
            waiter.state = E_WaiterState.WaitClientPay;
            SendNotification(OrderSystemEvent.UPDATE_WAITER_WAIT_CLIENT_HELLO, Waiters);
        }

        public void setWaiterIdleByOrder(ClientItem clientData)
        {//服务员恢复休息状态
            WaiterItem waiter = getWaiterByServerClient(clientData);
            if (waiter!=null)
            {
                waiter.state = E_WaiterState.Idle;
               // waiter.serverClientitem = null;
               // waiter.serverOrder = null;
                SendNotification(OrderSystemEvent.GET_PAY, Waiters);
            }
            else
            {
                Debug.LogError("setWaiterIdleByOrder not found waiter by ordre");
            }
        }

        private WaiterItem getWaiterByServerClient(ClientItem client)
        {
            if (client != null)
            {
            foreach (WaiterItem item in Waiters)
            {
                if(item.serverClientitem == client)//(item.serverOrder!=null&&item.serverCilentitem==client)
                {
                    return item;
                }
            }
            }
            
            return null;
        }

        public void serWaiterWaitMenu(WaiterItem waiter)
        {
            waiter.state = E_WaiterState.WaiterClientMenu;
         
            SendNotification(OrderSystemEvent.UPDATE_WAITER_VIEW, Waiters);
        }


        public WaiterItem getIdealWaiter()
        {
            WaiterItem waiter = null;
            foreach (WaiterItem item in Waiters)
            {
                if(item.state==E_WaiterState.Idle)
                {
                   
                    waiter = item;
                    break;
                }
                else
                {
                    Debug.LogError("没有空闲服务员，请稍等");
                }
            }
            return waiter;
        }
        
        public WaiterItem serverMenuToClient(ClientItem clientItem,Order order)
        {
            WaiterItem waiter =  setWaiterBusy(E_WaiterState.SendMenu, clientItem);
            waiter.serverOrder = order;

            return waiter; 
        }
        

        public WaiterItem serverOrderToClient(ClientItem clientItem)
        {
            return setWaiterBusy(E_WaiterState.SendOrder, clientItem);
        }
        public void waitSendFood(Order order)
        {
            WaiterItem waiter = getWaiterWaitOredr(order);
            waiter.state = E_WaiterState.Idle;
            SendNotification(OrderSystemEvent.WAIT_SEND_FOOD, Waiters);
        }
        private WaiterItem getWaiterWaitOredr(Order order)
        {
            if (order != null)
            {
                foreach (WaiterItem item in Waiters)
                {
                    if (item.serverOrder == order)//(item.serverOrder!=null&&item.serverCilentitem==client)
                    {
                        return item;
                    }
                }
            }

            return null;
        }

        public void serverFoodToClient(ClientItem clientItem)
        {
            WaiterItem waiterItem = setWaiterBusy(E_WaiterState.SendFood, clientItem);
            SendNotification(OrderSystemEvent.FOOD_TO_CLIENT, Waiters);
        }


        public WaiterItem setWaiterBusy(E_WaiterState state,ClientItem clientItem)
        {
            WaiterItem waiter = getIdealWaiter();
            if (waiter == null)
            {
                Debug.LogError("服务员因为没有空闲服务员为null");
            }
            else
            {
                waiter.serverClientitem = clientItem;
                if (waiter.serverClientitem == null||clientItem==null)
                {
                    Debug.LogError("传入设置client为空");
                }
                else
                {
                    waiter.state = state;
                }

            }
           // waiter.serverClientitem = clientItem;
            //waiter.state = state;

            return waiter;
        }

        public IList<WaiterItem> Waiters
        {
            get { return (IList<WaiterItem>) base.Data; }
        }

        public WaiterProxy() : base(NAME,new List<WaiterItem>())
        {
            AddWaiter(new WaiterItem(1 , "小丽", E_WaiterState.Idle));
            AddWaiter(new WaiterItem(2 , "小红", E_WaiterState.Idle));
            AddWaiter(new WaiterItem(3 , "小花", E_WaiterState.Idle));
        }

        public void AddWaiter( WaiterItem item )
        {
            Waiters.Add(item);
        }
        public void RemoveWaiter( WaiterItem item ) 
        {
            Waiters.Remove(item);
        }
    }
}